package com.share.example.spider.pipeline;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.share.example.spider.model.db.FundStock;
import com.share.example.spider.service.IFundStockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.List;

/**
 * 数据入库
 */
@Slf4j
@Component
public class FundStockPipeline implements Pipeline {
    @Autowired
    private IFundStockService iFundStockService;

    @Override
    public void process(ResultItems resultItems, Task task) {
        int status = 0;
        List<FundStock> data = resultItems.get(FundStock.class.getName());
        if (data == null || data.size() == 0) {
            log.info("爬取为空");
            return;
        }

        //删除数据
        boolean remove = iFundStockService.remove(Wrappers.<FundStock>query().eq("fund_Code", data.get(0).getFundCode()));

        //数据入库
        boolean save = iFundStockService.saveBatch(data);
    }
}
